package com.shadt.cloud.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shadt.cloud.service.ChannelService;
import com.shadt.cloud.vo.ChannelVo;
import com.shadt.core.model.Json;

/**
 * 栏目控制器
 * 
 * @author SongR
 *
 */
@Controller
@RequestMapping(value = "/channel")
public class ChannelController {

	@Autowired
	ChannelService service;

	/**
	 * 获取所有栏目
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/all", method = RequestMethod.GET)
	public Json getAll() {
		Json j = new Json();
		List<ChannelVo> vos = new ArrayList<ChannelVo>();
		try {
			vos = service.getAll();
		} catch (Exception e) {
		}
		j.setObj(vos);
		return j;
	}
	
	/**
	 * 分页获取父级节点
	 */
	@ResponseBody
	@RequestMapping(value = "/parent", method = RequestMethod.GET)
	public Json getParent(HttpServletRequest request) {
		String start =request.getParameter("start")==null?"":request.getParameter("start");
		String size= request.getParameter("size")==null?"":request.getParameter("size");
		Json j = new Json();
		List<ChannelVo> vos = new ArrayList<ChannelVo>();
		try {
			vos = service.getParent(start,size);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(vos.size()>0){
			j.setObj(vos);
			j.setMsg("成功获取所有父节点");
			j.setSuccess(true);
		}else{
			j.setMsg("暂无栏目");
			j.setSuccess(true);
		}
		return j;
	}

	/**
	 * 根据父节点获取所有子节点
	 * @param request
	 * @param pid
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/child/{pid}", method = RequestMethod.GET)
	public Json getChild(HttpServletRequest request,@PathVariable String pid) {
		String start =request.getParameter("start")==null?"":request.getParameter("start");
		String size= request.getParameter("size")==null?"":request.getParameter("size");
		String id = request.getParameter("id");
		String idarry = pid+","+id;
		Json j = new Json();
		List<ChannelVo> vos = new ArrayList<ChannelVo>();
		try {
			vos = service.getChild(idarry,start,size);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(vos.size()>0){
			j.setObj(vos);
			j.setMsg("成功获取子节点");
			j.setSuccess(true);
		}else{
			j.setMsg("暂无栏目");
			j.setSuccess(true);
		}
		return j;
	}

}
